package com.siwei.mes.entity;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONArray;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.config.UidUtils;
import com.siwei.mes.enums.GenerateCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.List;


/**
 * 试验台账抽样(ExperimentSampleSmoke)实体类
 *
 * @author linzi
 * @since 2023-12-28 20:47:31
 */
@Data
@Accessors(chain = true)
@Table(name = "t_experiment_sample_smoke")
@ApiModel(value = "ExperimentSampleSmoke", description = "试验台账抽样")
public class ExperimentSampleSmoke implements Serializable {
    private static final long serialVersionUID = 235031943186091915L;
    /**
     * 主键id
     */
    @Id
    @ApiModelProperty(value = "试验台账抽样主键ID", name = "id")
    @NotNull(groups = {UpdateGroup.class}, message = "id不能为空")
    private Long id;
    /**
     * 抽样编号
     */
    @Column(name = "smoke_no")
    @ApiModelProperty(value = "抽样编号", name = "smokeNo")
    private String smokeNo;


    @Column(name = "smoke_no_alias")
    @ApiModelProperty(value = "编号别称")
    private String smokeNoAlias;

    /**
     * 试验台账id
     */
    @Column(name = "experiment_id")
    @ApiModelProperty(value = "试验台账id", name = "experimentId")
    @NotNull(groups = {AddGroup.class, UpdateGroup.class}, message = "试验台账id不能为空")
    private Long experimentId;
    /**
     * 样品用途
     */
    @Column(name = "sample_use")
    @ApiModelProperty(value = "样品用途", name = "sampleUse")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "样品用途不能为空")
    private String sampleUse;
    /**
     * 取样数量
     */
    @Column(name = "sample_quantity")
    @ApiModelProperty(value = "取样数量", name = "sampleQuantity")
    @NotNull(groups = {AddGroup.class, UpdateGroup.class}, message = "取样数量不能为空")
    private Double sampleQuantity;
    /**
     * 抽样人id
     */
    @Column(name = "sample_person_id")
    @ApiModelProperty(value = "抽样人id", name = "samplePersonId")
    private Long samplePersonId;
    /**
     * 抽样人名称
     */
    @Column(name = "sample_person_name")
    @ApiModelProperty(value = "抽样人名称", name = "samplePersonName")
    private String samplePersonName;
    /**
     * 抽样时间
     */
    @Column(name = "sample_time")
    @ApiModelProperty(value = "抽样时间", name = "sampleTime")
    private Date sampleTime;
    /**
     * 样品图片  多个用逗号隔开
     */
    @Column(name = "sample_img")
    @ApiModelProperty(value = "样品图片  多个用逗号隔开", name = "sampleImg")
    private String sampleImg;

    @Column(name = "sample_address")
    @ApiModelProperty(value = "抽样位置")
    private String sampleAddress;

    /**
     * 试验项目code  试验项目表
     */
    @Column(name = "test_project_code")
    @ApiModelProperty(value = "试验项目code  字典表", name = "testProjectCode")
    private String testProjectCode;
    /**
     * 试验项目名称
     */
    @Column(name = "test_project_name")
    @ApiModelProperty(value = "试验项目名称", name = "testProjectName")
    private String testProjectName;

    @Column(name = "sample_test_project_code")
    @ApiModelProperty(value = "对应抽样试验项目code  t_experiment_sample_test_project")
    private String sampleTestProjectCode;

    @Column(name = "sample_test_project_name")
    @ApiModelProperty(value = "对应抽样试验项目名称  t_experiment_sample_test_project")
    private String sampleTestProjectName;

    @Column(name = "freq_number")
    @ApiModelProperty(value = "执行频次")
    private Integer freqNumber;

    @Column(name = "freq_unit")
    @ApiModelProperty(value = "频次单位")
    private String freqUnit;

    @Column(name = "sample_test_project_json")
    @ApiModelProperty(value = "对应抽样试验项目json")
    private JSONArray sampleTestProjectJson;

    @Column(name = "is_test_block")
    @ApiModelProperty(value = "是否是试块 1-是 0-不是")
    private Integer isTestBlock;

    /**
     * 创建人
     */
    @Column(name = "creater")
    @ApiModelProperty(value = "创建人", name = "creater")
    private Long creater;
    /**
     * 创建时间
     */
    @Column(name = "create_time")
    @ApiModelProperty(value = "创建时间", name = "createTime")
    private Date createTime;
    /**
     * 修改人
     */
    @Column(name = "updater")
    @ApiModelProperty(value = "修改人", name = "updater")
    private Long updater;
    /**
     * 更新时间
     */
    @Column(name = "update_time")
    @ApiModelProperty(value = "更新时间", name = "updateTime")
    private Date updateTime;
    /**
     * 是否删除  1-删除
     */
    @Column(name = "is_del")
    @ApiModelProperty(value = "是否删除  1-删除", name = "isDel")
    private Integer isDel;

    @Column(name = "is_finish")
    @ApiModelProperty(value = "是否完成 1-已完成 0-未完成")
    private Integer isFinish;

    @Column(name = "finish_time")
    @ApiModelProperty(value = "完成时间")
    private Date finishTime;

    @ApiModelProperty(value = "处置人id")
    @Column(name = "disposer_person_id")
    private Long disposerPersonId;

    @ApiModelProperty(value = "处置人名称")
    @Column(name = "disposer_person_name")
    private String disposerPersonName;

    @Transient
    @ApiModelProperty(value = "多个抽样信息")
    private List<ExperimentSampleSmoke> smokeList;

    public ExperimentSampleSmoke init(boolean isCreate) {
        this.setUpdater(StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null)
                .setUpdateTime(new Date());
        if (isCreate) {
            this.setCreater(StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null)
                    .setCreateTime(new Date()).setIsFinish(0)
                    .setIsDel(0).setSmokeNo(UidUtils.genNo(GenerateCodeEnum.EXPERIMENT_SAMPLE_SMOKE.getCode()));
        }
        return this;
    }

    public ExperimentSampleSmoke tglInit(boolean isCreate) {
        this.setUpdateTime(new Date());
        if (isCreate) {
            this.setCreateTime(new Date()).setIsFinish(0)
                    .setIsDel(0).setSmokeNo(UidUtils.genNo(GenerateCodeEnum.EXPERIMENT_SAMPLE_SMOKE.getCode()));
        }
        return this;
    }
}
